﻿using System.Windows.Forms;
using CarlosAg.ExcelXmlWriter;
using SdmxMl.Common;

namespace SdmxMl.Excel
{
    /// <summary>
    /// Derived class specialized for flat hierarchy export
    /// </summary>
    public class ExcelExportFlatHierarchy : ExcelExportBase
    {
        #region FillRow helper Methods

        protected void CreateRows(FlatHierarchicalList fhl)
        {
            TreeNode root = new TreeNode();
            fhl.FillTree(root);

            int level = 0;
            foreach (TreeNode node in root.Nodes)
                FillRow(null, node, level);
        }

        /// <summary>
        /// In order to complete more detailled info like in concept
        /// </summary>
        /// <param name="row"></param>
        /// <param name="item"></param>
        protected virtual void FillAdditional(WorksheetRow row, ISdmxHierarchyListItem item)
        {
        }

        protected void FillRow(ISdmxHierarchyListItem parentItem, TreeNode node, int level)
        {
            ISdmxHierarchyListItem item = node.Tag as ISdmxHierarchyListItem;
            WorksheetRow row = sheet.Table.Rows.Add();
            AddCell(row, item.Id);
            AddCell(row, parentItem != null ? parentItem.Id : string.Empty);
            AddCell(row, (level + 1).ToString());

            foreach (string lg in SdmxMl.Common.LangSupport.SupportedLanguages)
                AddCell(row, item.Name.GetIfDefinedForLanguage(lg));

            MultiLang desc = GetDescription(item);
            if (desc != null && desc.IsEmpty == false)
            {
                foreach (string lg in SdmxMl.Common.LangSupport.SupportedLanguages)
                    AddCell(row, desc.GetIfDefinedForLanguage(lg));
            }
            else
            {
                foreach (string lg in SdmxMl.Common.LangSupport.SupportedLanguages)
                    AddCell(row, string.Empty);
            }

            FillAdditional(row, item);

            // All annotations
            AddAnnotations(row, item.AnnotationList);

            // Recurse
            foreach (TreeNode n in node.Nodes)
                FillRow(item, n, level + 1);
        }
        #endregion

        #region virtual method

        protected virtual MultiLang GetDescription(object ob)
        {
            return null;
        }
        #endregion

    }
}
